﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using CalendarParser.ForexNewsServiceReference;

namespace CalendarParser
{
    public partial class ForexClock : Form
    {
       

        ForexNewsServiceReference.ForexNewsServiceClient client = new CalendarParser.ForexNewsServiceReference.ForexNewsServiceClient();

        public ForexClock()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblDate.Text = "News Alerts For " + DateTime.Now.ToLongDateString();
            
            
            DrawListViewfromNewsList(client.GetNewsListForToday());
            //LoadData();
        }

        string StripTags(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }

        string[] GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)
        {
            string[] result = { "", "" };
            int iIndexOfBegin = strSource.IndexOf(strBegin);

            if (iIndexOfBegin != -1)
            {
                if (includeBegin)
                {
                    iIndexOfBegin -= strBegin.Length;
                }

                strSource = strSource.Substring(iIndexOfBegin + strBegin.Length);
                int iEnd = strSource.IndexOf(strEnd);

                if (iEnd == -1)
                {
                    iEnd = strSource.IndexOf("table");

                }

                if (iEnd != -1)
                {
                    if (includeEnd)
                    {
                        iEnd += strEnd.Length;
                    }
                    result[0] = strSource.Substring(0, iEnd);

                    if (iEnd + strEnd.Length < strSource.Length)
                    {
                        result[1] = strSource.Substring(iEnd + strEnd.Length);
                    }
                }
                
            }
            else
            {
                result[1] = strSource;
            }
            return result;
        }

        string GetDate(int day, bool first)
        {
            DateTime time;
            string format;

            if (first)
            {
                format = "MMM d";
            }
            else
            {
                format = "d MMM";
            }

            if (day == 0)
            {
                time = DateTime.Now;
            }
            else
            {
                time = DateTime.Now.AddDays((double)day);
            }
            return time.ToString(format);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            //LoadData();
        }

      

        void DrawListViewfromNewsList(List<ForexNews> newslist)
        {
            if (listView1.Items.Count > 0)
            {
                listView1.Items.Clear();
            }

            if (newslist.Count > 0)
            {
                for (int i = 0; i < newslist.Count; i++)
                {
                    listView1.Items.Add(new ListViewItem(new string[] { newslist[i].NewsTime, newslist[i].NewsValue }));
                }
            }
        }

      
    }
}
